/**
 * 
 */
package com.newtribe.security.cert;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.newtribe.security.remote.WorkerInstanceRemote;
import com.newtribe.security.util.FilePathUtil;

/**
 * @author newtribe
 *
 */
public class DomainFactory {
	
	public static void main(String[] argvs )throws Exception {
		DomainFactory factory =DomainFactory.getFactory() ;
		WorkerInstanceRemote instance =factory.getWorkerInstance("default") ;
		System.out.println(instance);
		CAConfig ca =instance.getCaConfig();
	
		
	}
	
	private DomainFactory(){
		
	}
	private static DomainFactory factory =new DomainFactory();
	
	private String instanceConfigFile=FilePathUtil.getUserDir()+"/src/designs/ca实例.xml";
	
	private Map<String ,WorkerInstance>  instancs =new HashMap<String ,WorkerInstance> () ;
	
	/**
	 * @return the instancs
	 */
	public Map<String, WorkerInstance> getWorkerInstancs() {
		return instancs;
	}

	/**
	 * @param instancs the instancs to set
	 */
	public void addWorkerInstancs(WorkerInstance instance) {
		this.instancs.put(instance.getId(), instance) ;
	}
	
	public WorkerInstance getWorkerInstance(String id ) {
		return this.instancs.get(id) ;
	}

	public static DomainFactory  getFactory() throws Exception {
		factory.init() ;
		
		return factory ;
	}
	/**
	 * user instance config file init the factory 
	 * @throws Exception
	 */
	private  void init() throws Exception {
		
		WorkerInstance instance =new WorkerInstance();
		InputStream in =new FileInputStream(instanceConfigFile);
		WorkerInstance config =instance.init(in) ;
		this.addWorkerInstancs(config) ;
		in.close();
		
	}
	

}
